﻿#pragma once
#include<SFML/Graphics.hpp>
#include <vector>
#include "../../libcat/include/util/json.h"
#include "../../libcat/include/cat-interaction.hpp"
class gamepad {
	struct key_save 
	{
		std::vector<int> keys;
		bool isShow = false;
		bool isLastShow = false;
		unsigned int LastShowCount = 0;
		sf::Time record;
	};
	struct joy_axis {
		float value;
	};
public:
	static gamepad* GetInstance();

	static void ReleaseInstance();

	void Init(Json::Value cfg);

	void Update(Json::Value cfg);

	void OnJoyButtonPress(const cat_joystick_button_event* event, bool up);

	void OnJoyAxisMove(const cat_joystick_axis_move_event* event);

	void OnKeyPress(const cat_key_event* event, bool key_up);

	void Draw(void* _window);

	void Release();

protected:
	gamepad();

	~gamepad();

private:
	void drawCat(sf::RenderTexture& _window);
	void drawKey(sf::RenderTexture& _window);
	void drawAxis(sf::RenderTexture& _window);
	void drawLeftHand(sf::RenderTexture& _window);
	void drawRightHand(sf::RenderTexture& _window);
	void drawFace(sf::RenderTexture& _window);
	void playSound();
	void playMotion();
	void playExpression();

	void drawline(sf::RenderTexture& window, std::vector<double> pss2, int red_value, int green_value, int blue_value);

	bool isInit;

	int renderWindowWidth, renderWindowHeight;
	sf::RenderTexture renderTexture;
	sf::Sprite renderSprite;

	bool isLive2d;
	double l2d_correct;
	double l2d_offset_x, l2d_offset_y;
	bool l2d_random_motion;
	bool motion_keep;
	bool sound_keep;
	bool l2d_random_expression;

	bool l3_flage,r3_flage,l2_flage,r2_flage,l3_down_flag,r3_down_flag;

	int red_value;
	int green_value;
	int blue_value;

	sf::Clock key_clock;

	sf::Sprite bg, cat, left_up, right_up,
		left_stick,left_stick_down,
		right_stick,right_stick_down,
		l2,r2,l2_hand,r2_hand;

	sf::Texture rightHandTex,leftHandTex;

	std::map<int, bool> states;
	std::map<int, bool> joy_states;

	std::map<int, joy_axis> axis_states;

	std::map<std::string, key_save> axis;
	std::map<std::string, key_save> left_hands_down;
	std::map<std::string, key_save> right_hands_down;
	std::map<std::string, key_save> keys;
	std::map<std::string, key_save> axiskeys;
	std::map<std::string, key_save> faces;
	std::map<std::string, key_save> sounds;
	std::map<std::string, key_save> motions;
	std::map<std::string, key_save> expression;

	int gamePadId;
	double X_axis;
};



